from model import Spider
from datetime import datetime
from config import db
from mail import send_mail
from threading import Thread

class BaseSpider:
    def __init__(self, spider):
        self.url = spider.url
        self.id = spider.id
        self.target_price = spider.target_price

    def getData(self):
        pass

    def setToDb(self, website):
        obj = self.getData()

        spider = Spider.query.get(self.id)
        spider.title = obj['title']
        spider.sku = obj['sku']
        spider.price = float(obj['price'][1:])
        spider.updated_at = datetime.now()
        if(spider.price <= spider.target_price):
            print('===================================================')
            print(spider.price <= spider.target_price)
            print(spider.price,  spider.target_price)
            print(spider.title)
            spider.status = False
            Thread(target=send_mail, args=(website, spider.title, spider.url,)).start()
            print('************************************************************')
        db.session.commit()